﻿
using System.Collections.Generic;
using System.Data;
using System;
using ZHDJ.WebPage;

namespace ZHDJ.MobileInterface
{
    public partial class UploadPicAndCreatePhoto : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            UploadPicAndCreatePhotoOutParam result = new UploadPicAndCreatePhotoOutParam();
            UploadPicAndCreatePhotoInParam param = new UploadPicAndCreatePhotoInParam();
            string logMsg = "";
            bool isFlag = true;

            //UploadPicAndCreatePhotoInParam pp = new UploadPicAndCreatePhotoInParam();
            //pp.data = new PhotoList();
            //Photo p = new Photo();
            //p.img = "1";
            //p.img_name = "2";
            //p.isfirst = "3";
            //pp.data.list.Add(p);
            //p = new Photo();
            //p.img = "11";
            //p.img_name = "22";
            //p.isfirst = "33";
            //pp.data.list.Add(p);
            //pp.desc = "desc";
            //pp.name = "name";
            //pp.type_id = "type";
            //pp.user_id = "user;";
            //string test = SerializeHelper.JsonSerializer<UploadPicAndCreatePhotoInParam>(pp);
            try
            { 
                param = inParam as UploadPicAndCreatePhotoInParam;
                if (string.IsNullOrEmpty(param.user_id) || string.IsNullOrEmpty(param.type_id) || string.IsNullOrEmpty(param.name)
                    || param.data.list == null || param.data.list.Count == 0)
                {
                    result.code = "0";
                    result.desc = "用户ID，类型ID，相册名称，上传照片不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (CommonUtils.IsDisableUsers(param.user_id))
                    {
                        result.code = "0";
                        result.desc = "用户状态有问题，请联系管理员";

                        isFlag = false;
                    }
                }
                //if (string.IsNullOrEmpty(param.isThematicImg) || (param.isThematicImg !="0" && param.isThematicImg !="1"))
                //{
                //    result.code = "0";
                //    result.desc = "请设置该相册是否为专题图片";

                //    isFlag = false;
                //}
                 
                if (isFlag)
                {
                    //用户是否存在
                    isFlag = InterfaceHelper.CheckLoginIDCountEx(param.user_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "该用户不存在";
                    }
                }
                //param.type_id = "党员活动";
                if (isFlag)
                {
                    isFlag = InterfaceHelper.CheckPartyPhotoType(param.type_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "该留影墙类别不存在 ";
                    }
                }

                if (isFlag)
                { 
                    if (param.isThematicImg == "1")
                    {
                        isFlag = false;
                        result.code = "0";
                        result.desc = "暂不支持专题图片上传 ";
                    }
                }

                if (isFlag)
                {
                    foreach (Photo item in param.data.list)
                    {
                        if (item.img_name.IndexOf(".") <= 0)
                        {
                            result.code = "0";
                            result.desc = "照片名称不规范，请使用带后缀的照片名称";

                            isFlag = false;
                        }
                    }
                }

                UpFileInfo fileTemp = new UpFileInfo();
                List<UpFileInfo> fileList = new List<UpFileInfo>();
                if (isFlag)
                { 
                    foreach (Photo item in param.data.list)
                    {
                        //上传文件 
                        fileTemp = new UpFileInfo(); 
                        UpFileResult upResult = UpFileBusiness.Upload(Convert.FromBase64String(item.img), item.img_name, param.user_id, "");

                        if (!upResult.IsSuccess || string.IsNullOrEmpty(upResult.FileID))
                        {
                            result.code = "0";
                            result.desc = "照片上传失败";

                            isFlag = false;
                            break;
                        }

                        fileTemp.fileName = item.img_name;
                        fileTemp.url = upResult.FileID;
                        item.img = "";//照片内容太多，影响数据库性能

                        if (item.isfirst == "1")
                            fileList.Insert(0, fileTemp);
                        else
                            fileList.Add(fileTemp); 
                    }
                }
                else
                {
                    foreach (Photo item in param.data.list)
                    {
                        item.img = "";//照片内容太多，影响数据库性能
                    }
                }

                if (isFlag)
                {
                    //保存数据
                    if (fileList.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "没有上传有效相片";

                        isFlag = false;
                    }
                    else
                    {
                        InterfaceHelper.InsertPhotoCreateAndPicUploadInfo(param.user_id, param.type_id, param.name, param.desc,param.isThematicImg, fileList);
                    }
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }
              
            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-UploadPicAndCreatePhoto", SerializeHelper.JsonSerializer<UploadPicAndCreatePhotoInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            //ProcessRequest<UploadPicAndCreatePhotoInParam, UploadPicAndCreatePhotoOutParam>();
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<UploadPicAndCreatePhotoInParam, UploadPicAndCreatePhotoOutParam>();
            else
                SafeProcessRequest<UploadPicAndCreatePhotoInParam, UploadPicAndCreatePhotoOutParam>();
        }
    }

    #region 请求参数

    public class UploadPicAndCreatePhotoInParam
    {
        public UploadPicAndCreatePhotoInParam()
        {
            data = new PhotoList();
        }

        /// <summary>
        ///	用户名		
        /// </summary>
        public string user_id
        { get; set; }  
    
        /// <summary>
        ///	类型ＩＤ		
        /// </summary>
        public string type_id
        { get; set; }  
    
        /// <summary>
        ///	相册名称		
        /// </summary>
        public string name
        { get; set; }

        /// <summary>
        /// 相片列表
        /// </summary>
        public PhotoList data
        {
            get;
            set;
        }
    
        /// <summary>
        ///	相册描述		
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        ///	是否专题图片		
        /// </summary>
        public string isThematicImg
        { get; set; }  
    }

    public class PhotoList
    {
        public PhotoList()
        {
            list = new List<Photo>();
        }
        public List<Photo> list
        {
            get;
            set;
        }
    }
    #endregion

    #region 结果参数
    public class UploadPicAndCreatePhotoOutParam
    {
        public UploadPicAndCreatePhotoOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; } 
    }  
    #endregion 
}